﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.SessionState;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using ZX.EHR.Library;
using Newtonsoft.Json;
namespace ZXMOBIWEB.Proc
{
    /// <summary>
    /// GetData 的摘要说明
    /// </summary>
    public class GetData : IHttpHandler, IReadOnlySessionState
    {
         
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["TYPE"];
            string para = context.Request["PARA"];
            context.Response.Write(Proc(type,para));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private string Proc(string type, string para)
        {
            string back = "";
            switch (type)
            {
                case "getdatalist":
                    back = GetDataList(para);
                    break;
                case "teacalendar":
                    back = GetTeachCalendar(para);
                    break;
                case "calendarContent":
                    back = GetTeachCalContent(para);
                    break;
                case "getdept":
                    back = BackDept();
                    break;
                case "getcustomer":
                    back = GetCustomerByid(para);
                    break;
                case "loadsumdata":
                    back = GetSumData(para);//
                    break;
                case "saveteasum1":
                    back = SaveSum(para,1);//保存讲师行程
                    break;
                case "saveteasum0":
                    back = SaveSum(para, 0);//保存讲师行程
                    break;
            }
            return back;
        }
        private string GetTeachCalendar(string para)
        {
            string[] pas = para.Split('︾');
            BTCrmTeachHodometer btth = new BTCrmTeachHodometer();
            return btth.GetTeachCal(pas[0],pas[1]);
        }
        private string GetTeachCalContent(string para)
        {
            string[] pas = para.Split('︾');
            BTCrmTeachHodometer btth = new BTCrmTeachHodometer();
            DataTable dt= btth.GetTeachContent(pas[0], pas[1]);
            string str = JsonConvert.SerializeObject(dt);
            return str;
        }

        private string BackDept()
        {
            BTCrmTeachHodometer btth = new BTCrmTeachHodometer();
            return JsonConvert.SerializeObject(btth.getDept());
        }

        private string GetSumData(string para)
        {
            BTCrmTeachHodometer btth = new BTCrmTeachHodometer();
            DataTable dt = btth.GetThodometerId(para);
            string back = "";
            if (dt.Rows.Count > 0)
            {
                DataTable dt3 = btth.getDept();
                DataTable dt2 = btth.GetCrmTeachSumupByid(dt.Rows[0]["ThodometerId"].ToString());
                if (dt2.Rows.Count > 0)
                {
                    back = "{\"Dept\":" + JsonConvert.SerializeObject(dt3) + ",\"TeachSumup\":" + JsonConvert.SerializeObject(dt2) + "}";
                }
                else
                {
                    DataTable dt1 = btth.GetCustomerByid(dt.Rows[0]["CusID"].ToString());
                    DataTable dt4 = btth.GetV_CrmCustomerOrder(dt.Rows[0]["CusID"].ToString());
                    back = "{\"Dept\":" + JsonConvert.SerializeObject(dt3) + ",\"Customer\":" + JsonConvert.SerializeObject(dt1) + ",\"Order\":" + JsonConvert.SerializeObject(dt4) + ",\"Thodometer\":" +JsonConvert.SerializeObject(dt) + "}";
                }
            }
            return back;
        }

        private string SaveSum(string str,int type)
        {
            BCrmTeachSumup Bbussness = new BCrmTeachSumup();
            Bbussness.entity = new CrmTeachSumup();
            V_CrmTeaSumSave TeachSumup = JsonConvert.DeserializeObject<V_CrmTeaSumSave>(str);
            return Bbussness.ExecProc(TeachSumup, type);
        }

        private string GetDataList(string para)
        {
            string back = "";
            string[] arr = para.Split('︾');
            BTCrmTeachHodometer btth = new BTCrmTeachHodometer();
            DataTable dt1 = btth.GetColNameByEntity(arr[0]);
            if (dt1.Rows.Count > 0)
            {
                DataTable dt2 = btth.GetColEntitys(arr[1], arr[0], dt1);
                if (dt2.Rows.Count > 0)
                    back = "{\"TableName\":" + JsonConvert.SerializeObject(dt1) + ",\"TableData\":" + JsonConvert.SerializeObject(dt2) + "}";
            }
            return back;
        }

        private string GetCustomerByid(string cusid)
        {
            BTCrmTeachHodometer btth = new BTCrmTeachHodometer();
            return JsonConvert.SerializeObject(btth.GetCustomerByid(cusid));
        }
    }
}